package com.qianchang.microfinance.setting;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.event.LoginEvent;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.AppManager;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.MainActivity;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengConfirmAlertDialog;
import com.qianchang.microfinance.view.login.LoginActivity;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineLinearLayout;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 设置
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:30
 */

public class SettingActivity extends BaseActivity {

    private static final String TAG = "SettingActivity";
    @BindView(R.id.oll_password_management)
    OneLineLinearLayout ollPasswordManagement;
    @BindView(R.id.oll_helper_center)
    OneLineLinearLayout ollHelperCenter;
    @BindView(R.id.oll_feed_back)
    OneLineLinearLayout ollFeedBack;
    @BindView(R.id.oll_about)
    OneLineLinearLayout ollAbout;
    @BindView(R.id.btn_logout)
    Button btnLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_setting));
        btnLogout.setEnabled(true);
    }


    @OnClick({R.id.oll_password_management, R.id.oll_helper_center, R.id.oll_feed_back, R.id.oll_about, R.id.btn_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.oll_password_management:
                startActivity(PasswordManagerActivity.class);
                break;
            case R.id.oll_helper_center:
                startActivity(HelpCenterActivity.class);
                break;
            case R.id.oll_feed_back:
                startActivity(FeedbackActivity.class);
                break;
            case R.id.oll_about:
                startActivity(AboutActivity.class);
                break;
            case R.id.btn_logout:
                DimengConfirmAlertDialog confirmAlertDialog = DimengConfirmAlertDialog.newInstance("", getString(R.string.msg_make_sure_logout));
                confirmAlertDialog.setAgreeText(getString(R.string.btn_sure));
                confirmAlertDialog.setOnDialogClickListener(new DimengConfirmAlertDialog.OnDialogClickListener() {
                    @Override
                    public void agree() {
                        logout();
                    }

                    @Override
                    public void cancel() {

                    }
                });
                confirmAlertDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
                break;
        }
    }

    private void logout() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.LOGOUT, params, new RequestCallBack<String>() {//UserLoanInfo
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (response.isReqDataSuccess()) {
                    setOnProfileSignOff();
                    DMApplication.getInstance().setUserLoginInfo(null);
                    AppManager.getAppManager().finishOtherActivities(MainActivity.class);
                    startActivity(LoginActivity.class);
                    EventBus.getDefault().post(new LoginEvent(LoginEvent.LoginStatue.LOGOUT));
                } else {
                    ToastUtil.getInstant().show(SettingActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(SettingActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }

    //友盟统计应用自身的账号 登出调用 账号登出时需调用此接口，调用之后不再发送账号相关内容
    private void setOnProfileSignOff(){
        //登出
        MobclickAgent.onProfileSignOff();
    }


}
